package be.phl.mycinema;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
import be.phl.mycinema.fragments.AanbiedingFragment;
import be.phl.mycinema.fragments.AccountFragment;
import be.phl.mycinema.fragments.FilmFragment;

public class MainActivity extends FragmentActivity implements OnClickListener, OnMenuItemClickListener {
	
	private static SectionsPagerAdapter mSectionsPagerAdapter;
	private static ViewPager mViewPager;
	private static TextView input;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		mViewPager.setCurrentItem(1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.menu_search:
			AlertDialog.Builder alert = new Builder(this);
			input = new EditText(this);

			alert.setTitle("Film zoeken");
			alert.setMessage("Titel :");
			alert.setView(input);
			alert.setPositiveButton("Ok", this);

			alert.show();
			break;
		case R.id.menu_settings:
			showPopup(findViewById(R.id.menu_settings));
		}
		
		return super.onOptionsItemSelected(item);
	}

	public void showPopup(View v) {
		
		PopupMenu popup = new PopupMenu(this, v);
		popup.inflate(R.menu.popup_menu);
		popup.setOnMenuItemClickListener(this);
		popup.show();
		
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter {
		
		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			Fragment fragment = null;

			switch (i) {
			case 0:
				fragment = new AanbiedingFragment();
				break;
			case 1:
				fragment = new FilmFragment();
				break;
			case 2:
				fragment = new AccountFragment();
				break;
			}

			return fragment;
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			switch (position) {
			case 0:
				return getString(R.string.title_section1).toUpperCase();
			case 1:
				return getString(R.string.title_section2).toUpperCase();
			case 2:
				return getString(R.string.title_section3).toUpperCase();
			}
			return null;
		}
	}

	public void onClick(DialogInterface dialog, int which) {
		
		Intent i = new Intent(this, SearchActivity.class);
		
		if(input.getText().toString().equals("")) {
			Toast.makeText(this, "Geef een zoekterm in!", Toast.LENGTH_SHORT).show();
			return;
		}
		
		i.putExtra("SEARCHVALUE", input.getText().toString());
		startActivity(i);
	}

	public boolean onMenuItemClick(MenuItem item) {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Bent u zeker dat u wilt uitloggen?");
		
		builder.setNegativeButton("Neen", null);
		builder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
				SharedPreferences prefs = getSharedPreferences("be.phl.mycinema.userinfo", MODE_PRIVATE);
				Editor prefEditor = prefs.edit();
				prefEditor.clear();
				prefEditor.commit();

				Intent i = new Intent(getBaseContext(),LoginActivity.class);
				startActivity(i);
			}
		});
		
		builder.show();
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		if (! Utils.isNetworkAvailable((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE))) {
			Toast.makeText(getBaseContext(), "Er is geen internetverbinding beschikbaar!", Toast.LENGTH_LONG).show();
			finish();
		} else if (! Utils.isWebserviceAvailable()) {
			Toast.makeText(getBaseContext(), "De server is niet bereikbaar, probeer later opnieuw.", Toast.LENGTH_LONG).show();
			finish();
		}
	}
	
	@Override
	public void onBackPressed() {
		// Back button disabled
	}
}
